from django.db import models
from django.conf import settings


class Course(models.Model):
    """课程信息"""
    WEEKDAY_CHOICES = [
        (1, '星期一'),
        (2, '星期二'),
        (3, '星期三'),
        (4, '星期四'),
        (5, '星期五'),
        (6, '星期六'),
        (7, '星期日'),
    ]

    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    course_name = models.CharField(max_length=100, verbose_name='课程名称')
    teacher = models.CharField(max_length=50, null=True, blank=True, verbose_name='教师')
    location = models.CharField(max_length=100, null=True, blank=True, verbose_name='上课地点')
    weekday = models.IntegerField(choices=WEEKDAY_CHOICES, verbose_name='星期')
    start_time = models.TimeField(verbose_name='开始时间')
    end_time = models.TimeField(verbose_name='结束时间')
    start_week = models.IntegerField(default=1, verbose_name='开始周')
    end_week = models.IntegerField(default=18, verbose_name='结束周')
    class_number = models.CharField(max_length=50, null=True, blank=True, verbose_name='课程代码')
    credit = models.FloatField(null=True, blank=True, verbose_name='学分')
    notes = models.TextField(null=True, blank=True, verbose_name='备注')
    color = models.CharField(max_length=7, default='#409EFF', verbose_name='课程颜色')
    reminder_enabled = models.BooleanField(default=True, verbose_name='是否开启提醒')
    reminder_minutes = models.IntegerField(default=30, verbose_name='提前提醒分钟数')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'course'
        verbose_name = '课程'
        verbose_name_plural = verbose_name
        ordering = ['weekday', 'start_time']
        indexes = [
            models.Index(fields=['user', 'weekday']),
        ]

    def __str__(self):
        return f'{self.course_name} - {self.get_weekday_display()}'


class CourseReminder(models.Model):
    """课程提醒记录"""
    STATUS_CHOICES = [
        ('pending', '待发送'),
        ('sent', '已发送'),
        ('failed', '发送失败'),
    ]

    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    reminder_time = models.DateTimeField(verbose_name='提醒时间')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    sent_at = models.DateTimeField(null=True, blank=True, verbose_name='发送时间')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'course_reminder'
        verbose_name = '课程提醒'
        verbose_name_plural = verbose_name
        ordering = ['-reminder_time']
        indexes = [
            models.Index(fields=['status', 'reminder_time']),
        ]

    def __str__(self):
        return f'{self.course.course_name} - {self.reminder_time}'


class Semester(models.Model):
    """学期信息"""
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    name = models.CharField(max_length=50, verbose_name='学期名称')
    start_date = models.DateField(verbose_name='开始日期')
    end_date = models.DateField(verbose_name='结束日期')
    is_current = models.BooleanField(default=False, verbose_name='是否当前学期')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'semester'
        verbose_name = '学期'
        verbose_name_plural = verbose_name
        ordering = ['-start_date']
        unique_together = ['user', 'name']

    def __str__(self):
        return f'{self.user.username} - {self.name}'
